<template>
  <el-dialog
    :title="!dataForm.menuId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="菜单名称" prop="menuName">
      <el-input v-model="dataForm.menuName" placeholder="菜单名称"></el-input>
    </el-form-item>
    <el-form-item label="父菜单ID" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父菜单ID"></el-input>
    </el-form-item>
    <el-form-item label="显示顺序" prop="orderNum">
      <el-input v-model="dataForm.orderNum" placeholder="显示顺序"></el-input>
    </el-form-item>
    <el-form-item label="路由地址" prop="path">
      <el-input v-model="dataForm.path" placeholder="路由地址"></el-input>
    </el-form-item>
    <el-form-item label="组件路径" prop="component">
      <el-input v-model="dataForm.component" placeholder="组件路径"></el-input>
    </el-form-item>
    <el-form-item label="路由参数" prop="query">
      <el-input v-model="dataForm.query" placeholder="路由参数"></el-input>
    </el-form-item>
    <el-form-item label="是否为外链（0是 1否）" prop="isFrame">
      <el-input v-model="dataForm.isFrame" placeholder="是否为外链（0是 1否）"></el-input>
    </el-form-item>
    <el-form-item label="是否缓存（0缓存 1不缓存）" prop="isCache">
      <el-input v-model="dataForm.isCache" placeholder="是否缓存（0缓存 1不缓存）"></el-input>
    </el-form-item>
    <el-form-item label="菜单类型（M目录 C菜单 F按钮）" prop="menuType">
      <el-input v-model="dataForm.menuType" placeholder="菜单类型（M目录 C菜单 F按钮）"></el-input>
    </el-form-item>
    <el-form-item label="菜单状态（0显示 1隐藏）" prop="visible">
      <el-input v-model="dataForm.visible" placeholder="菜单状态（0显示 1隐藏）"></el-input>
    </el-form-item>
    <el-form-item label="菜单状态（0正常 1停用）" prop="status">
      <el-input v-model="dataForm.status" placeholder="菜单状态（0正常 1停用）"></el-input>
    </el-form-item>
    <el-form-item label="权限标识" prop="perms">
      <el-input v-model="dataForm.perms" placeholder="权限标识"></el-input>
    </el-form-item>
    <el-form-item label="菜单图标" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="菜单图标"></el-input>
    </el-form-item>
    <el-form-item label="创建者" prop="createBy">
      <el-input v-model="dataForm.createBy" placeholder="创建者"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="更新者" prop="updateBy">
      <el-input v-model="dataForm.updateBy" placeholder="更新者"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="updateTime">
      <el-input v-model="dataForm.updateTime" placeholder="更新时间"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          menuId: 0,
          menuName: '',
          parentId: '',
          orderNum: '',
          path: '',
          component: '',
          query: '',
          isFrame: '',
          isCache: '',
          menuType: '',
          visible: '',
          status: '',
          perms: '',
          icon: '',
          createBy: '',
          createTime: '',
          updateBy: '',
          updateTime: '',
          remark: ''
        },
        dataRule: {
          menuName: [
            { required: true, message: '菜单名称不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父菜单ID不能为空', trigger: 'blur' }
          ],
          orderNum: [
            { required: true, message: '显示顺序不能为空', trigger: 'blur' }
          ],
          path: [
            { required: true, message: '路由地址不能为空', trigger: 'blur' }
          ],
          component: [
            { required: true, message: '组件路径不能为空', trigger: 'blur' }
          ],
          query: [
            { required: true, message: '路由参数不能为空', trigger: 'blur' }
          ],
          isFrame: [
            { required: true, message: '是否为外链（0是 1否）不能为空', trigger: 'blur' }
          ],
          isCache: [
            { required: true, message: '是否缓存（0缓存 1不缓存）不能为空', trigger: 'blur' }
          ],
          menuType: [
            { required: true, message: '菜单类型（M目录 C菜单 F按钮）不能为空', trigger: 'blur' }
          ],
          visible: [
            { required: true, message: '菜单状态（0显示 1隐藏）不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '菜单状态（0正常 1停用）不能为空', trigger: 'blur' }
          ],
          perms: [
            { required: true, message: '权限标识不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '菜单图标不能为空', trigger: 'blur' }
          ],
          createBy: [
            { required: true, message: '创建者不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          updateBy: [
            { required: true, message: '更新者不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.menuId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.menuId) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysmenu/info/${this.dataForm.menuId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.menuName = data.sysMenu.menuName
                this.dataForm.parentId = data.sysMenu.parentId
                this.dataForm.orderNum = data.sysMenu.orderNum
                this.dataForm.path = data.sysMenu.path
                this.dataForm.component = data.sysMenu.component
                this.dataForm.query = data.sysMenu.query
                this.dataForm.isFrame = data.sysMenu.isFrame
                this.dataForm.isCache = data.sysMenu.isCache
                this.dataForm.menuType = data.sysMenu.menuType
                this.dataForm.visible = data.sysMenu.visible
                this.dataForm.status = data.sysMenu.status
                this.dataForm.perms = data.sysMenu.perms
                this.dataForm.icon = data.sysMenu.icon
                this.dataForm.createBy = data.sysMenu.createBy
                this.dataForm.createTime = data.sysMenu.createTime
                this.dataForm.updateBy = data.sysMenu.updateBy
                this.dataForm.updateTime = data.sysMenu.updateTime
                this.dataForm.remark = data.sysMenu.remark
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ruoyiapisystem/sysmenu/${!this.dataForm.menuId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'menuId': this.dataForm.menuId || undefined,
                'menuName': this.dataForm.menuName,
                'parentId': this.dataForm.parentId,
                'orderNum': this.dataForm.orderNum,
                'path': this.dataForm.path,
                'component': this.dataForm.component,
                'query': this.dataForm.query,
                'isFrame': this.dataForm.isFrame,
                'isCache': this.dataForm.isCache,
                'menuType': this.dataForm.menuType,
                'visible': this.dataForm.visible,
                'status': this.dataForm.status,
                'perms': this.dataForm.perms,
                'icon': this.dataForm.icon,
                'createBy': this.dataForm.createBy,
                'createTime': this.dataForm.createTime,
                'updateBy': this.dataForm.updateBy,
                'updateTime': this.dataForm.updateTime,
                'remark': this.dataForm.remark
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
